﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using SerialPort;

namespace DevicesWPF.UCDevices
{
    /// <summary>
    /// UCSerialPortConfig.xaml 的交互逻辑
    /// </summary>
    public partial class UCSerialPortConfig : UserControl
    {

        EncodingInfo[] infos;
        SerialPortConfig spc;
        public UCSerialPortConfig()
        {
            InitializeComponent();
            infos = Encoding.GetEncodings();
            LoadData();
        }


        public UCSerialPortConfig(SerialPortConfig config)
        {
            InitializeComponent();
            infos = Encoding.GetEncodings();
            spc = config;
            LoadData();
        }
        public void LoadData(SerialPortConfig config)
        {
            spc = config;
            UCSerialPort_Load(null, null);
        }


        private void UCSerialPort_Load(object sender, RoutedEventArgs e)
        {
            if (this.spc != null)
            {
                comboBoxComs.SelectedItem = spc.PortName;
                comboBoxBaud.SelectedItem = spc.Baud;
                comboBoxParity.SelectedItem = spc.Parity;
                comboBoxStopBits.SelectedItem = spc.StopBits;
                textBoxDataBits.Text = spc.DataBits.ToString();

                EncodingInfo info = infos.FirstOrDefault(o => o.Name == spc.EncodingName);
                if(info!=null)
                comboBoxEncodeing.SelectedValue = info;
            }
        }
        public SerialPortConfig GetSerialPortConfig()
        {
            SerialPortConfig config = new SerialPortConfig();
            config.PortName = comboBoxComs.SelectedValue?.ToString();
            config.Baud = Convert.ToInt32(comboBoxBaud.SelectedValue);
            config.Parity = (Parity)comboBoxParity.SelectedItem;
            config.StopBits = (StopBits)comboBoxStopBits.SelectedItem;

            config.EncodingName = (comboBoxEncodeing.SelectedValue as EncodingInfo)?.Name;

            config.DataBits = int.Parse(this.textBoxDataBits.Text);
            return config;
        }

        private void LoadData()
        {
            string []coms= MySerialPort.GetPortNames();
            this.comboBoxComs.ItemsSource = coms;
            if (coms.Length > 0)
                comboBoxComs.SelectedIndex = 0;
            this.comboBoxBaud.ItemsSource = new int[] { 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200 };
            comboBoxBaud.SelectedIndex = 2;
            this.comboBoxParity.ItemsSource = Enum.GetValues(typeof(Parity));
            this.comboBoxStopBits.ItemsSource = Enum.GetValues(typeof(StopBits));
            comboBoxStopBits.SelectedIndex = 1;

            this.comboBoxEncodeing.ItemsSource = infos;
            this.comboBoxEncodeing.DisplayMemberPath = "DisplayName";
            EncodingInfo info = infos.FirstOrDefault(o=>o.Name=="utf-8");
            comboBoxEncodeing.SelectedValue = 0;
            if (info!=null)
            comboBoxEncodeing.SelectedValue = info;
        }


    }
}
